package WebIde

import (
	"context"
	clients "go-devops/client"
	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/api/core/v1"
	networkv1 "k8s.io/api/networking/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"log"
)

func CreateWebVscode(deploymentName, namespace string) {
	k8sClient := clients.InitK8sConfig()
	_, err := k8sClient.AppsV1().
		Deployments(namespace).Create(
		context.Background(),
		&appsv1.Deployment{
			TypeMeta: metav1.TypeMeta{
				Kind:       "Deployment",
				APIVersion: "apps/v1",
			},
			ObjectMeta: metav1.ObjectMeta{
				Name:      deploymentName,
				Namespace: namespace,
			},
			Spec: appsv1.DeploymentSpec{
				Replicas: func() *int32 { var i int32; i = 1; return &i }(),
				Selector: &metav1.LabelSelector{
					MatchLabels: map[string]string{"app": deploymentName},
				},
				Template: v1.PodTemplateSpec{
					ObjectMeta: metav1.ObjectMeta{
						Labels: map[string]string{"app": deploymentName},
					},
					Spec: v1.PodSpec{
						Containers: []v1.Container{
							{
								Name:  deploymentName,
								Image: "192.168.1.240:5000/code-server:v1",
								Ports: []v1.ContainerPort{
									{
										Name:          "code-sever",
										ContainerPort: 9999,
										Protocol:      v1.ProtocolTCP,
									},
								},
							},
						},
					},
				},
			},
		},
		metav1.CreateOptions{})
	if err != nil {
		log.Fatal(err)
	}
}

func GetIngressSvcNodeport() (nodeport int) {
	ns := "nginx-ingress"
	svc := "my-nginx-ingress-nginx-controller"
	k8sClient := clients.InitK8sConfig()
	ingressSvc, err := k8sClient.CoreV1().Services(ns).Get(context.Background(), svc, metav1.GetOptions{})
	if err != nil {
		log.Fatal(err)
	}
	for _, port := range ingressSvc.Spec.Ports {
		if port.Name == "http" {
			nodeport = int(port.NodePort)
		}
	}
	return
}

func CreateWebVscodeIngress(deploymentName, namespace string) (url string) {
	className := "nginx"
	k8sClient := clients.InitK8sConfig()
	prefix := networkv1.PathTypePrefix
	url = "http://" + deploymentName + "-vscode.test.com"
	ingress := &networkv1.Ingress{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Ingress",
			APIVersion: "networking.k8s.io/v1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:      deploymentName,
			Namespace: namespace,
		},
		Spec: networkv1.IngressSpec{
			IngressClassName: &className,
			Rules: []networkv1.IngressRule{
				{
					Host: deploymentName + "-vscode.test.com",
					IngressRuleValue: networkv1.IngressRuleValue{
						HTTP: &networkv1.HTTPIngressRuleValue{
							Paths: []networkv1.HTTPIngressPath{
								{
									Path:     "/",
									PathType: &prefix,
									Backend: networkv1.IngressBackend{
										Service: &networkv1.IngressServiceBackend{
											Name: deploymentName,
											Port: networkv1.ServiceBackendPort{
												Number: int32(9999),
											},
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
	_, err := k8sClient.NetworkingV1().Ingresses(namespace).Create(context.Background(), ingress, metav1.CreateOptions{})
	if err != nil {
		log.Println(err)
	}
	return
}
